package com.sduept.nwld.secondaryequipment.evaluation.rest;

import com.alibaba.fastjson.JSONObject;
import com.sduept.cim.model.protection.ProtectionEquipment;
import com.sduept.cim.wave.service.ProtectionEquipmentManager;
import com.sduept.core.dao.SuperDAO;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;
import java.util.Map.Entry;
@RestController
@RequestMapping("/protectdata")
public class ProtectDataService {
	@Autowired
	private SuperDAO dao;
	@Autowired
	private ProtectionEquipmentManager p;

	/**
	 * 获取全网保护统计信息
	 * @return 
	 */

	@GetMapping("/getProtectInfo")
	public String getProtectInfo() {
		JSONObject jso = new JSONObject();
		// 获取所有保护
		List<ProtectionEquipment> alls = p.getAllCimProtectEquipment();
		// 获取所有保护类型
		List<String> protecedTypes = p.listALLProtectedTypes();
		// 电压等级统计
		int list500Num = 0;
		int list220Num = 0;
		int list110Num = 0;
		int list35Num = 0;
		// 生产厂家统计{厂家:[数量1，数量2，数量3]},按总计降序排列
		Map<String, int[]> manufacturerMap = new HashMap<String, int[]>();
		for (ProtectionEquipment protectEquipment : alls) {
			Integer voltage = protectEquipment.getStandvoltage();
			String manufacturer = protectEquipment.getDevManufacturer();
			String protecedType = protectEquipment.getProtectedType();
			if (null != voltage) {
				if (voltage == 500) {
					list500Num++;
				} else if (voltage >= 220 && voltage < 500) {
					list220Num++;
				} else if (voltage >= 110 && voltage < 220) {
					list110Num++;
				} else if (voltage >= 35 && voltage < 110) {
					list35Num++;
				}
			}
			if (!StringUtils.isEmpty(manufacturer) && !StringUtils.isEmpty(protecedType)) {
				int i = protecedTypes.indexOf(protecedType);// 被保护类型在protecedTypes中的位置
				int[] numList = manufacturerMap.get(manufacturer);
				if (numList == null) {
					numList = new int[protecedTypes.size() + 1];
					manufacturerMap.put(manufacturer, numList);
				}
				numList[i]++;
			}
		}
		// 计算每个厂家的总数
		for (Entry<String, int[]> entry : manufacturerMap.entrySet()) {
			int[] values = entry.getValue();
			int sum = 0;
			for (int i = 0; i < values.length - 1; i++) {
				sum += values[i];
			}
			values[values.length - 1] = sum;
		}
		Map<String, Integer> map = new HashMap<>();
		map.put("500kV", list500Num);
		map.put("220kV", list220Num);
		map.put("110kV", list110Num);
		map.put("35kV", list35Num);
		map.put("other", alls.size() - list500Num - list220Num - list110Num - list35Num);
		map.put("total", alls.size());
		jso.put("vol", map);
		for (int i = 0; i < protecedTypes.size(); i++) {
			protecedTypes.set(i, protecedTypes.get(i) + "保护");
		}
		jso.put("protecedTypes", protecedTypes);
		// 按总计的降序排列
		List<Map.Entry<String, int[]>> list = new ArrayList<Map.Entry<String, int[]>>(manufacturerMap.entrySet());
		Collections.sort(list, new Comparator<Map.Entry<String, int[]>>() {
			@Override
			public int compare(Entry<String, int[]> o1, Entry<String, int[]> o2) {
				return o2.getValue()[o2.getValue().length - 1] - o1.getValue()[o1.getValue().length - 1];
			}
		});
		Map<String, int[]> result = new LinkedHashMap<String, int[]>();
		for (Map.Entry<String, int[]> entry : list) {
			result.put(entry.getKey(), entry.getValue());
		}
		jso.put("manufacturer", result);
		return jso.toJSONString();
	}

	/**
	 * 获取全网保护中含有空值的
	 * @return 
	 */

	@GetMapping("/getProtectWithNull")
	public String getProtectWithNull() {
		String sql = "select * from t_cim_protection_equipment t where t.sub_id is null or t.protected_id is null or t.dev_type is null or t.dev_manufacturer ='' or t.protected_type is null or t.sub_id is null";
		// String sql = "select
		// t.name,t.protected_id,t.protected_name,t.protected_type,t.sub_id,t.sub_name,t.dev_type,t.dev_manufacturer,t.pt_id
		// from t_cim_protection_equipment t where t.protected_id is null ";
		List<?> list = dao.nativeQuery(sql);
		return JSONObject.toJSONString(list);
	}

	/**
	 * 根据stationId获取所有一次设备
	 * @return {"breaker":{"name":"id","name":"id"},"line":{"name":"id","name":"id"},"BYQ":{}}
	 */

	@GetMapping("/getPrimDevByStationId/{stationId}")
	public String getPrimDevByStationId(@PathVariable("stationId") String stationId) {
		Map<String, Map<String, String>> map = new HashMap<>();
		// 断路器
		String sql1 = "select t.name,t.id from t_cim_breaker t where t.sub_id='" + stationId + "'";
		List<?> list = dao.nativeQuery(sql1);
		Map<String, String> map1 = new HashMap<>();
		map.put("断路器", map1);
		if (list.size() != 0) {
			for (Object os : list) {
				Object[] s1 = (Object[]) os;
				map1.put(s1[0].toString(), s1[1].toString());
			}
		}

		// 线路
		String sql2 = "select t.name,t.id from t_cim_aclinesegment t where t.substation='" + stationId + "'";
		list = dao.nativeQuery(sql2);
		map1 = new HashMap<>();
		map.put("线路", map1);
		if (list.size() != 0) {
			for (Object os : list) {
				Object[] s2 = (Object[]) os;
				map1.put(s2[0].toString(), s2[1].toString());
			}
		}
		// 变压器
		String sql3 = "select t.name,t.id from t_cim_powertransformer t where t.equipmentContainer='" + stationId + "'";
		list = dao.nativeQuery(sql3);
		map1 = new HashMap<>();
		map.put("变压器", map1);
		if (list.size() != 0) {
			for (Object os : list) {
				Object[] s3 = (Object[]) os;
				map1.put(s3[0].toString(), s3[1].toString());
			}
		}
		return JSONObject.toJSONString(map);
	}
}
